import App, { Container } from "next/app";
import Link from "next/link";
import { Layout, Menu, Icon, Avatar, Spin } from "antd";
import router from "next/router";
import "antd/dist/antd.css";
import { withRouter } from "next/router";
const { Header, Footer } = Layout;
import * as TYEPS from "../store/action-types";
import axios from "../utils/axios";
import createStore from "../store";
import { Provider } from "react-redux";
const __REDUX_STORE__ = "__REDUX_STORE__";
function getStore(initialState) {
  if (typeof window == "undefined") {
    //如果 在服务器端运行的。那么直接创建新仓库返回
    return createStore(initialState);
  } else {
    //如果此代码是在客户端执行的,第一次会创建，以后每次都复用上一次创建的
    if (!window[__REDUX_STORE__]) {
      window[__REDUX_STORE__] = createStore(initialState);
    }
    return window[__REDUX_STORE__];
  }
}
class LayoutApp extends App<any> {
  store: any;
  routeChangeStart: any;
  routeChangeComplete: any;
  state = {
    loading: false,
  };
  constructor(props) {
    super(props);
    console.log("constructor LayoutApp");

    //此构造函数只会在客户端执行一次 这里的initialState是getInitialProps返回的
    this.store = getStore(props.initialState);
  }
  // 在页面级别渲染时只会被执行一次，即服务端渲染或每次切换客户端渲染都会被执行，但服务端渲染时客户端不会执行
  static async getInitialProps({ Component, ctx }) {
    let store = getStore({});
    let pageProps = {};
    console.log("2. getInitialProps");
    let options: any = {
      url: "/api/currentUser",
    };
    //如果此方法是在服务器执行的，那么会有ctx.req属性，它代表本次node请求对象
    if (ctx.req && ctx.req.headers.cookie) {
      options.headers = options.headers || {};
      options.headers.cookie = ctx.req.headers.cookie;
    }
    let response = await axios(options);
    if (response.data.code === 0) {
      // 当前登录的用户
      let currentUser = response.data.data;
      store.dispatch({ type: TYEPS.SET_USER_INFO, payload: currentUser });
    }
    if (Component.getInitialProps) {
      // 执行当前页面的getInitialProps
      let data = await Component.getInitialProps(ctx);
      pageProps = { ...data };
    }
    return { pageProps, initialState: store.getState() };
  }
  componentDidMount() {
    this.routeChangeStart = (url) => {
      this.setState({ loading: true });
    };
    this.routeChangeComplete = (url) => {
      this.setState({ loading: false });
    };
    router.events.on("routeChangeStart", this.routeChangeStart);
    router.events.on("routeChangeComplete", this.routeChangeComplete);
  }
  componentWillUnmount() {
    router.events.off("routeChangeStart", this.routeChangeStart);
    router.events.off("routeChangeComplete", this.routeChangeComplete);
  }
  render() {
    console.log("3.LayoutApp.render");
    let { Component, pageProps } = this.props as any;
    let { currentUser } = this.store.getState();
    let pathname = this.props.router.pathname;
    pathname = "/" + pathname.split("/")[1];
    return (
      <Provider store={this.store}>
        <style jsx>
          {`
            a {
              display: inline-block !important;
            }
          `}
        </style>
        <Layout>
          <Header className="header">
            <Menu
              theme="dark"
              mode="horizontal"
              style={{ lineHeight: "64px", display: "inline-block" }}
              selectedKeys={[pathname]}
              defaultSelectedKeys={[pathname]}
            >
              <Menu.Item key="/">
                <Icon type="home" />
                <Link href="/">
                  <a>首页</a>
                </Link>
              </Menu.Item>
              <Menu.Item key="/user">
                <Icon type="/user" />{" "}
                <Link href="/user">
                  <a>用户管理</a>
                </Link>
              </Menu.Item>
              <Menu.Item key="/profile">
                <Icon type="profile" />
                <Link href="/profile">
                  <a>个人中心</a>
                </Link>
              </Menu.Item>
              <Menu.Item key="/login">
                <Icon type="login" />
                <Link href="/login">
                  <a>登录</a>
                </Link>
              </Menu.Item>
            </Menu>
            {currentUser && (
              <div
                style={{
                  display: "inline-block",
                  float: "right",
                  color: "red",
                }}
              >
                <Avatar style={{ color: "#F00", backgroundColor: "#CCC" }}>
                  {currentUser.username}
                </Avatar>
              </div>
            )}
          </Header>
          {this.state.loading ? (
            <Spin style={{ fontSize: 50, margin: "50px auto" }} />
          ) : (
            <Component {...pageProps} />
          )}
          <Footer style={{ textAlign: "center" }}>@copyright wzyan</Footer>
        </Layout>
      </Provider>
    );
  }
}
export default withRouter(LayoutApp);
